# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2020/9/13 11:43
desc: 登录页面page object
'''

import logging,time

from selenium.webdriver.common.by import By
from base.pypom import Page
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from base.ui_func import readiness_of_document


class LoginPage(Page):
    URL_TEMPLATE = '/'

    _tab_standard_account = (By.ID, 'rc-tabs-0-tab-2')
    _input_username = (By.ID, 'username')
    _input_password = (By.ID, 'password')
    _button_login = (By.CSS_SELECTOR, 'button.ant-btn.login-button.ant-btn-primary.ant-btn-lg')

    @property
    def loaded(self):
        title = self.find_element(By.CLASS_NAME,'login-title')
        return title.is_displayed()

    def switch_standard_account_tab(self):
        self.click(*self._tab_standard_account)

    def type_username(self, username):
        self.input(self._input_username, username)

    def type_password(self, password):
        self.input(self._input_password, password)

    def click_login_btn(self):
        self.click(*self._button_login)

    def login(self,account_type,username,password):
        """登录web page

        :param account_type: 账号类型：0-LDAP，1-Standard
        :param username: 用户名
        :param password: 密码

        :return: 无
        """
        self.get(self.URL_TEMPLATE)
        self.driver.maximize_window()

        WebDriverWait(self.driver, 10).until(
            #EC.presence_of_element_located((By.ID, 'rc-tabs-0-tab-2'))
            EC.presence_of_element_located((By.ID, 'rc-tabs-0-tab-1'))
        )

        #self.wait_for_page_to_load()

        if account_type==1: # standard account
            self.switch_standard_account_tab()

        self.type_username(username)
        #self.driver.execute_script("document.getElementsByClassName('ant-input ant-input-lg')[0].dispatchEvent(new Event('input'))")
        self.type_password(password)

        self.click_login_btn()
        #WebDriverWait(self.driver, 10).until(readiness_of_document())
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.home_page')))


